package com.stone.redis.lock.api.controller.v1;

import com.stone.redis.lock.app.service.RedisFairLockTestService;
import com.stone.redis.lock.domain.bo.LockInfo;
import com.stone.starter.core.util.result.Results;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 测试分布式锁
 *
 * @author Mr_wenpan@163.com 2021/09/09 14:01
 */
@Slf4j
@RestController("TestRedisLockController.v1")
@RequestMapping("/v1/test-lock")
public class TestRedisLockController {

    @Autowired
    private RedisFairLockTestService redisFairLockTestService;
    @Autowired
    private Hello hello;
    private final AtomicInteger atomicInteger = new AtomicInteger(0);

    @GetMapping("/test-01")
    @ApiOperation(value = "测试")
    public ResponseEntity<?> test01() throws InterruptedException {
        redisFairLockTestService.test();
        return Results.success("success");
    }

    @GetMapping("/test-02")
    @ApiOperation(value = "测试2")
    public ResponseEntity<?> test02() throws InterruptedException {
        LockInfo lockInfo = new LockInfo("", atomicInteger.get(), atomicInteger.get(), TimeUnit.SECONDS);
        System.out.println("atomicInteger = " + atomicInteger.get());
        atomicInteger.getAndIncrement();
        hello.setLockInfo(lockInfo);

        TimeUnit.SECONDS.sleep(5);

        System.out.println("lockInfo = " + hello.getLockInfo());
        System.out.println("hello = " + hello);
        System.out.println("hello hashcode " + hello.hashCode());
        return Results.success("success");
    }

}
